//
//  RMMOrderDetailsViewController.m
//  Romomo
//
//  Created by puman on 2016/10/24.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMOrderDetailsViewController.h"
#import "RMMOrderDeatilHeadView.h"
#import "RMMOderDeatilsTableViewCell.h"
#import "RMMOrderDetailGoodsTableViewCell.h"
#import "RMMOderYouHuiTableViewCell.h"
#import "RMMOrderCommonBottomView.h"
#import "RMMTuiKuanViewController.h"
#import "RMMTimerView.h"
#import "RMMSelcetPickerView.h"
#import "RMMPayOrderViewController.h"
#import "RMMOrderEvaluationViewController.h"
#import "RMMXiuGaiOrderView.h"
#import "RMMPayJieGuoViewController.H"
#import "RMMChosseDateViewController.h"
#import "RMMChooseSkillViewController.h"
#import "RMMOrderJiShiView.h"
#import "RMMAddBookingOrderViewController.h"
#import "RMMGoodsConfirmViewController.h"
#import "RMMShopingModel.h"
#import "RMMHomeShareView.h"

@interface RMMOrderDetailsViewController ()<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource,RMMCommonAlertViewDelegate, RMMXiuGaiOrderViewDelegate>

@end

@implementation RMMOrderDetailsViewController
{
    RMMSelcetPickerView *_pickerView;
    RMMXiuGaiOrderView *_xiugaiView;
    RMMTimerView *_timerView;
    RMMOrderDeatilHeadView *_headView;
    RMMOrderCommonBottomView *_bottomView;
    UITableView *_tableView;
    UILabel *_countLabel;
    UILabel *_addressLabel;
    RMMOrderJiShiView *_jiShiView;
    RMMNotNetWorkView *_networkView;
    NSArray *_dataArray;
    NSDictionary *_dataDic;
    NSString *_cancelremak;
    NSInteger _number;
    NSArray *_array;
    BOOL _isSuccess;
    NSInteger _time;
    NSInteger _row;
    NSString *_phoneNumber;
    CGFloat _headSectionHeight;
    RMMHomeShareView *_shareVIew;
    NSString *_titleName;
    NSString *_desTitleName;
    UIButton *_hBut;
}
- (void)dealloc {
    NS_REM(@"OREDERDETAILS", nil);
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[RMMFileManager shareHandle].bookingDic removeAllObjects];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.navigationItem.title = @"订单详情";
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud setHidden:YES];
        [self addViews];
        [self getData];
    });
    // Do any additional setup after loading the view.
}
- (void)getData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"order_id"] = _order_id;
    parDic[@"is_show_prods"] = @"1";
    NSString *url = PayOrderDetailUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (isNetwork) {
            _networkView.hidden = YES;
        } else {
            _networkView.hidden = NO;
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _bottomView.hidden = NO;
            NSDictionary *dic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            [_model setValuesForKeysWithDictionary:dic];
            _dataDic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            _dataArray = dic[@"ORDER_SERVERS"];
            _headView.time = dic[@"SERVER_TIME"];
            _phoneNumber = dataDic[@"data"][@"EMP_MOBILE"];
            if ([_model.SOURCE isEqualToString:@"3"] || [_model.SOURCE isEqualToString:@"0"]) {
                if ([dic[@"BOOK_STATUS"] isEqualToNumber:@1] || [dic[@"BOOK_STATUS"] isEqualToNumber:@2]) {
                    _headSectionHeight = 120;
                    _headView.type = @"1";
                    _headView.typeName = @"待服务";
                    [self leftButBWith:@"取消订单"];
                    [self rigBut];
                } else if ([dic[@"BOOK_STATUS"] isEqualToNumber:@3]){
                    _headSectionHeight = 120;
                    _headView.type = @"2";
                    _headView.typeName = @"服务中";
                    [self leftButBWith:@"客服下单"];
                    [self rightButBWith:@"加单"];
                } else if ([dic[@"BOOK_STATUS"] isEqualToNumber:@4]) {
                    //待评价
                    _headSectionHeight = 120;
                    _headView.time = @"待评价";
                    _headView.type = @"3";
                    _headView.typeName = @"待评价";
                    [self leftButAWith:@"评价获得颜值分"];
                    _bottomView.leftButton.layer.borderWidth = 1;
                    [self rigBut];
                } else if ([dic[@"BOOK_STATUS"] isEqualToNumber:@5]){
                    // 已完成
                    _headSectionHeight = 44;
                    _headView.type = @"4";
                    _headView.time = @"已完成";
                    _headView.typeName = @"已完成";
                    [self leftButBWith:@""];
                    [self rigBut];
                } else if ([dic[@"BOOK_STATUS"] isEqualToNumber:@9]) {
                    // 已取消
                    _headSectionHeight = 44;
                    _headView.type = @"4";
                    _headView.time = @"已取消";
                    _headView.typeName = @"已取消";
                    [self leftButBWith:@""];
                    [self rigBut];
                }
            } else {
                if ([dic[@"ORDER_STATUS"] isEqualToNumber:@0] || [dic[@"ORDER_STATUS"] isEqualToNumber:@1]) {
                    if ([dic[@"BOOK_STATUS"] isEqualToNumber:@0] || [dic[@"BOOK_STATUS"] isEqualToNumber:@1] || [dic[@"BOOK_STATUS"] isEqualToNumber:@2] || [dic[@"BOOK_STATUS"] isEqualToNumber:@3] || dic[@"BOOK_STATUS"] == nil) {
                        // 进行中
                        _headSectionHeight = 120;
                        if ([dic[@"ORDER_STATUS"] isEqualToNumber:@0]) {
                            _headView.type = @"0";
                            _headView.typeName = @"待支付";
                            [self leftButBWith:@"取消订单"];
                            [self rightButAWith:@"去支付"];
                        } else {
                            if ([dic[@"BOOK_STATUS"] isEqualToNumber:@1] || [dic[@"BOOK_STATUS"] isEqualToNumber:@2]) {
                                _headView.type = @"1";
                                _headView.typeName = @"待服务";
                                if ([_model.IS_UPDATE isEqualToNumber:@0]) {
                                    [self leftButBWith:@"申请退款"];
                                    [self rightButBWith:@"修改预约"];
                                } else {
                                    [self leftButBWith:@"申请退款"];
                                    [self rigBut];
                                }
                            } else {
                                _headView.type = @"2";
                                _headView.typeName = @"服务中";
                                [self leftButBWith:@""];
                                [self rightButBWith:@"加单"];
                            }
                        }
                    }  else {
                        if ([dic[@"BOOK_STATUS"] isEqualToNumber:@4]) {
                            //待评价
                            _headSectionHeight = 120;
                            _headView.time = @"待评价";
                            _headView.type = @"3";
                            _headView.typeName = @"待评价";
                            [self leftButAWith:@"评价获得颜值分"];
                            _bottomView.leftButton.layer.borderWidth = 1;
                            [self rigBut];
                        } else {
                            // 已完成
                            _headSectionHeight = 44;
                            _headView.type = @"4";
                            _headView.time = @"已完成";
                            _headView.typeName = @"已完成";
                            [self leftButBWith:@""];
                            [self rigBut];
                        }
                    }
                } else if([dic[@"ORDER_STATUS"] isEqualToNumber:@8]){
                    // 退款中
                    _headSectionHeight = 44;
                    _headView.type = @"8";
                    _headView.typeName = @"退款中";
                    [self leftButBWith:@"退款详情"];
                    [self rigBut];
                    _headView.frame = CGRectMake(0, 0, kScreenWideth, 44);
                    _tableView.tableHeaderView = _headView;
                } else if ([dic[@"ORDER_STATUS"] isEqualToNumber:@10]) {
                    // 退款完成
                    _headSectionHeight = 44;
                    _headView.time = dic[@"LAST_MODI_DATE"];
                    _headView.type = @"10";
                    _headView.typeName = @"退款完成";
                    [self leftButBWith:@"退款详情"];
                    [self rigBut];
                    _headView.frame = CGRectMake(0, 0, kScreenWideth, 44);
                    _tableView.tableHeaderView = _headView;
                } else {
                    // 已取消
                    _headSectionHeight = 44;
                    _headView.type = @"9";
                    _headView.typeName = @"已取消";
                    _headView.frame = CGRectMake(0, 0, kScreenWideth, 44);
                    _tableView.tableHeaderView = _headView;
                    [self rigBut];
                    [self leftButBWith:@""];
                }
            }
            if ([dic[@"ORDER_STATUS"] isEqualToNumber:@1]) {
                _hBut.hidden = NO;
            } else {
                _hBut.hidden = YES;
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        NSString *emp_id = [RMMFileManager retrunObjWith:_model.EMP_ID];
        if (emp_id.length && ![emp_id isEqualToString:@"(null)"]) {
            [self getSkillDetails];
        }
        _headView.frame = CGRectMake(0, 0, kScreenWideth, _headSectionHeight);
        [_tableView reloadData];

    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)getSkillDetails{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"emp_id"] = _model.EMP_ID;
    NSString *url = GetSkillDetailUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _jiShiView.dic = dataDic[@"data"];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
    } error:^(NSError *errors) {
    } view:self.view];
}
- (void)leftButAWith:(NSString *)title{
    [_bottomView.leftButton setTitle:title forState:(UIControlStateNormal)];
    _bottomView.leftButton.frame = CGRectMake(_bottomView.width / 2 - 60, 10.5, 120, 30);
    _bottomView.leftButton.layer.borderColor = kBasicColor.CGColor;
}
- (void)leftButBWith:(NSString *)title{
    [_bottomView.leftButton setTitle:title forState:(UIControlStateNormal)];
    _bottomView.leftButton.frame = CGRectMake(15, 10.5, 74, 28);
    _bottomView.leftButton.layer.borderColor = kWhiteColor.CGColor;
}
- (void)rigBut{
    [_bottomView.but setTitle:@"" forState:(UIControlStateNormal)];
    _bottomView.but.backgroundColor = kWhiteColor;
    [_bottomView.but setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    _bottomView.but.layer.borderColor = kWhiteColor.CGColor;
}
- (void)rightButAWith:(NSString *)title{
    [_bottomView.but setTitle:title forState:(UIControlStateNormal)];
    _bottomView.but.backgroundColor = kBasicColor;
    _bottomView.but.layer.borderColor = kBasicColor.CGColor;
    [_bottomView.but setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
}
- (void)rightButBWith:(NSString *)title{
    [_bottomView.but setTitle:title forState:(UIControlStateNormal)];
    _bottomView.but.backgroundColor = kWhiteColor;
    [_bottomView.but setTitleColor:kBasicColor forState:(UIControlStateNormal)];
    _bottomView.but.layer.borderColor = kBasicColor.CGColor;
}

- (void)addViews{
    _row = 0;
    _headSectionHeight = 0.0;
    NS_ADD(update, @"OREDERDETAILS", nil);
    //[[[NSBundle mainBundle] loadNibNamed:@"OrderJiShiView" owner:self options:nil] lastObject];
    _bottomView = [[RMMOrderCommonBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 49 - 64, kScreenWideth, 49)];
    _bottomView.hidden = YES;
    BUTTON_S(_bottomView.leftButton, leftButtonClick:);
    BUTTON_S(_bottomView.but, butClick:);
    [self.view addSubview:_bottomView];

    _headView = [[RMMOrderDeatilHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 120)];
    _headView.isHome = _model.IS_HOME;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[RMMOderDeatilsTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [_tableView registerClass:[RMMOderYouHuiTableViewCell class] forCellReuseIdentifier:@"YouCell"];
    [_tableView registerClass:[RMMOrderDetailGoodsTableViewCell class] forCellReuseIdentifier:@"OderCell"];
    _tableView.tableHeaderView = _headView;
    
    _jiShiView = [[RMMOrderJiShiView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 54)];
    BUTTON_S(_jiShiView.but, callSkillBut);
    NSString *emp_id = [RMMFileManager retrunObjWith:_model.EMP_ID];
    if (emp_id.length && ![emp_id isEqualToString:@"(null)"]) {
        _tableView.tableFooterView = _jiShiView;
    }

    [self.view addSubview:_tableView];
    
    _array = @[@"忘记用优惠券喽",@"突然有事很不好意思",@"重新选定项目",@"需要换个美容师",@"手一哆嗦多拍了"];
    _pickerView = [[RMMSelcetPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _pickerView.pickerView.delegate = self;
    _pickerView.pickerView.dataSource = self;
    BUTTON_S(_pickerView.rihthBut, doneButClick);
    _pickerView.type = @"location";
    _pickerView.hidden = YES;
    [self.view addSubview:_pickerView];

    _xiugaiView = [[RMMXiuGaiOrderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kScreenHeight - 64)];
    _xiugaiView.hidden = YES;
    _xiugaiView.xiuGaiOrderDelegate = self;
    [self.view addSubview:_xiugaiView];
    _networkView = [[RMMNotNetWorkView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _networkView.hidden = YES;
    BUTTON_S(_networkView.button, networkBut);
    [self.view addSubview:_networkView];
    
    _shareVIew = [[RMMHomeShareView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight)];
    _shareVIew.hidden = YES;
    UIWindow *widow = [UIApplication sharedApplication].keyWindow;
    [widow addSubview:_shareVIew];
    // 分享按钮
    for (int i = 0; i < 5; i++) {
        UIButton *button = [_shareVIew viewWithTag:50 + i];
        BUTTON_S(button, shareButtonClick:);
    }
    _hBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _hBut.frame = CGRectMake(kScreenWideth - 65, kScreenHeight - 64 - 150 - 49, 50, 100);
    _hBut.hidden = YES;
    [_hBut setImage:[UIImage imageNamed:@"发红包@3x"] forState:(UIControlStateNormal)];
    [self.view addSubview:_hBut];
    BUTTON_S(_hBut, sendHBut);
}
- (void)sendHBut{
    [self getRedBagTitle];
}
- (void)networkBut{
    [self getData];
}
- (void)update{
    [self getData];
}
- (void)getRedBagTitle{
    [OSRequestManager requestWithUrlString:GetRedBagTitleUrl parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _shareVIew.hidden = NO ;
            [UIView animateWithDuration:kInterval animations:^{
                _shareVIew.view.frame = CGRectMake(0, kScreenHeight - 230, kScreenWideth, 230);
                _shareVIew.backView.alpha = 1;
            } completion:^(BOOL finished) {
                
            }];
            NSDictionary *dic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            _titleName = dic[@"RED_BAG_TITLE"];
            _desTitleName = dic[@"RED_BAG_BODY"];
        }
    } error:^(NSError *errors) {
        
    } view:self.view];
}
// 分享
- (void)shareButtonClick:(UIButton *)button{
    [UIView animateWithDuration:kInterval animations:^{
        _shareVIew.view.frame = CGRectMake(0, kScreenHeight, kScreenWideth, 230);
        _shareVIew.backView.alpha = 0.0;
    } completion:^(BOOL finished) {
        _shareVIew.hidden = YES;
    }];
    NSArray *array = @[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_Qzone)];
    
    UMSocialPlatformType platformType = - 8;
    platformType = [array[button.tag - 50] integerValue];
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    if (button.tag == 53) {
        // UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
        // shareObject.thumbImage = [UIImage imageNamed:@"Icon-iPhone-60@2x"];
        messageObject.text = [NSString stringWithFormat:@"http://ydywx.puman.cn/Web/RedBag/SendRedBag.ashx?action=RedShow&order_id=%@&type=0",_model.ORDER_ID];
        // messageObject.shareObject = shareObject;
    } else {
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:_titleName.length ? _titleName : @"容么么" descr:_desTitleName.length ? _desTitleName : @"亲爱的小主,容么么为你献上红包一枚!" thumImage:[UIImage imageNamed:@"Icon-iPhone-60@3x"]];
        //设置网页地址
        shareObject.webpageUrl = [NSString stringWithFormat:@"http://ydywx.puman.cn/Web/RedBag/SendRedBag.ashx?action=RedShow&order_id=%@&type=0",_model.ORDER_ID];
        messageObject.shareObject = shareObject;
    }
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id result, NSError *error) {
        
    }];
}

- (void)leftButtonClick:(UIButton *)button{
    NSString *title = [button titleForState:(UIControlStateNormal)];
    if ([title isEqualToString:@"取消订单"]) {
        _pickerView.hidden = NO;
        [UIView animateWithDuration:kInterval animations:^{
            _pickerView.view.frame = CGRectMake(0, kScreenHeight - 64 - 249, kScreenWideth, 249);
            _pickerView.backView.alpha = 1.0;
        } completion:^(BOOL finished) {
        }];

    } else if ([title isEqualToString:@"申请退款"]) {
        RMMTuiKuanViewController *VC = [[RMMTuiKuanViewController alloc] init];
        VC.type = @"申请退款";
        VC.order_id = _order_id;
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([title isEqualToString:@"评价获得颜值分"]) {
        RMMOrderEvaluationViewController *VC = [[RMMOrderEvaluationViewController alloc] init];
        VC.model = _model;
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([title isEqualToString:@"退款详情"]) {
        RMMTuiKuanViewController *VC = [[RMMTuiKuanViewController alloc] init];
        VC.order_id = _model.ORDER_ID;
        VC.type = @"退款结果";
        [self.navigationController pushViewController:VC animated:YES];
    }
}
// 取消订单
- (void)cancelOrderWith:(NSString *)cancelremak{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"order_id"] = _order_id;
    parDic[@"cancelremak"] = cancelremak;
    NSString *url = CancelOrderUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"取消成功!" title:nil afterDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
    } view:self.view];

}
- (void)butClick:(UIButton *)button{
    NSString *title = [button titleForState:(UIControlStateNormal)];
    if ([title isEqualToString:@"去支付"]) {
        RMMGoodsConfirmViewController *VC = [[RMMGoodsConfirmViewController alloc] init];
        VC.goodsArray = [NSMutableArray array];
        VC.order_id = [NSString stringWithFormat:@"%@",_model.ORDER_ID];
        VC.ORDER_NO = [NSString stringWithFormat:@"%@",_model.ORDER_NO];
        NSArray *array = _model.BOOK_DETAIL;
        for (NSDictionary *dic in array) {
            RMMShopingModel *models = [[RMMShopingModel alloc] init];
            models.SERVICE_ID = [RMMFileManager retrunObjWith:dic[@"SERVER_ID"]];
            models.IMAGEURL = dic[@"SER_IMG_URL"];
            models.SERVICE_NAME = dic[@"PROD_NAME"];
            models.COUNT = dic[@"PROD_CNT"];
            models.TIME_LEN = dic[@"TIME_LEN"];
            models.isSelect = @"1";
            models.MY_CARD_ID = @"0";
            models.PRICE = [NSString stringWithFormat:@"%@",dic[@"PROD_PRICE"]];
            models.AVA_QTY = [NSNumber numberWithInteger:[dic[@"PROD_CNT"] integerValue]];
            [VC.goodsArray addObject:models];
        }
        VC.isHome = _model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];

    } else if ([title isEqualToString:@"修改预约"]) {
        if ([_model.IS_HOME isEqualToString:@"1"]) {
            NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *nowTime = [formatter stringFromDate:date];
            if ([nowTime compare:_model.BEGIN_DATE] == NSOrderedDescending) {
                [RMMFileManager MBProgressWith:@"技师已出发,不能修改!" onView:self.view];
                return;
            }
        }
        [[RMMFileManager shareHandle].bookingDic setObject:[_dataDic objectForKey:@"SERVER_TIME"] forKey:@"时间"];
        if ([_model.IS_HOME isEqualToString:@"1"]) {
            [[RMMFileManager shareHandle].bookingDic setObject:@{@"ID":_model.ADDRESS_ID} forKey:@"地址"];
        } else {
            [[RMMFileManager shareHandle].bookingDic setObject:@{@"ID":_model.STORE_ID} forKey:@"地址"];
        }
        [[RMMFileManager shareHandle].bookingDic setObject:_order_id forKey:@"订单ID"];
        NSDictionary *dic = @{@"EMP_NAME":[_dataDic objectForKey:@"EMP_NAME"],@"EMP_ID":[_dataDic objectForKey:@"EMP_ID"]};
      //  [[RMMFileManager shareHandle].bookingDic setValue:dic forKey:@"技师"];
        [RMMFileManager shareHandle].totalTimeLength = _model.TIME_LEN;
        [self editorOrderAlertController];
    } else if ([title isEqualToString:@"加单"]) {
        [self GetCanUseTimeWith:_model];
    } else if ([title isEqualToString:@"再来一单"]) {
        RMMGoodsConfirmViewController *VC = [[RMMGoodsConfirmViewController alloc] init];
        NSArray *array = _model.BOOK_DETAIL;
        NSMutableArray *arrays = [NSMutableArray array];
        NSInteger timeLength = 0;
        for (NSDictionary *dic in array) {
            NSDictionary *dicM = [RMMFileManager setValueForDic:dic];
            _model.isSelect = @1;
            RMMShopingModel *models = [[RMMShopingModel alloc] init];
            models.SERVICE_ID = [RMMFileManager retrunObjWith:dic[@"SERVER_ID"]];
            models.IMAGEURL = dic[@"SER_IMG_URL"];
            models.SERVICE_NAME = dic[@"PROD_NAME"];
            models.COUNT = dic[@"PROD_CNT"];
            models.TIME_LEN = dic[@"TIME_LEN"];
            models.isSelect = @"1";
            NSString *my_card_id = dicM[@"MY_CARD_ID"];
            models.MY_CARD_ID = my_card_id.length == 0 ? @"0" : my_card_id;
            timeLength += [dic[@"TIME_LEN"] integerValue];
            models.PRICE = [NSString stringWithFormat:@"%@",dic[@"PROD_PRICE"]];
            models.AVA_QTY = [NSNumber numberWithInteger:[dic[@"PROD_CNT"] integerValue]];
            [arrays addObject:models];
        }
        VC.goodsArray = arrays;
        VC.isHome = _model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];
    }
}
// 加单
- (void)GetCanUseTimeWith:(RMMOrderModel *)model{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"emp_id"] = model.EMP_ID;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSDateFormatter *formaatter = [[NSDateFormatter alloc] init];
    [formaatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formaatter stringFromDate:date];
    parDic[@"query_time"] = [dateTime compare:model.END_DATE] == NSOrderedAscending ? model.END_DATE  : dateTime;
    NSString *url = GetEmpCanAddOrderTimeUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [self addBookingOrderWith:[RMMFileManager retrunObjWith:dataDic[@"data"]] model:model];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)addBookingOrderWith:(NSString *)time model:(RMMOrderModel *)model{
    // @"您预约的美容师还有 60 分钟可为您服务,是否要加单?"
    //
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上门服务加单" message:[NSString stringWithFormat:@"您预约的美容师还有 %@ 分钟可为您服务,是否要加单?",time]  preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        RMMAddBookingOrderViewController *VC = [[RMMAddBookingOrderViewController alloc] init];
        VC.model = model;
        VC.timeL = time;
        VC.isHome = model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];
    }];
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [self presentViewController:alertController animated:YES completion:nil];
}

// 修改订单
- (void)editorOrderAlertController{
    _xiugaiView.hidden = NO;
    [UIView animateWithDuration:kInterval animations:^{
        _xiugaiView.view.frame = CGRectMake(0, _xiugaiView.height - 188, _xiugaiView.width, 188);
        _xiugaiView.backView.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
    
}
- (void)touchButCount:(NSInteger)count{
    if (count == 0) {
        // 修改时间
        RMMChosseDateViewController *VC = [[RMMChosseDateViewController alloc] init];
        VC.type = @"2";
        VC.isHome = _model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];
    } else if (count == 1) {
        // 修改技师
        RMMChooseSkillViewController *VC = [[RMMChooseSkillViewController alloc] init];
        VC.type = @"2";
        VC.isHome = _model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];
    }
    [UIView animateWithDuration:kInterval animations:^{
        _xiugaiView.backView.alpha = 0.0;
        _xiugaiView.view.frame = CGRectMake(0, _xiugaiView.bottom,_xiugaiView.width, 188);
    } completion:^(BOOL finished) {
        _xiugaiView.hidden = YES;
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _dataArray.count;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 54;
    } else if (indexPath.section == 1){
        return 144;
    } else {
        return 213;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        UIView *hViwe =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 98)];
        hViwe.backgroundColor = kCColor;
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 24, 24)];
        imageV.image = [UIImage imageNamed:@"订单-地址图标@3x"];
        [hViwe addSubview:imageV];
        
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageV.right + 12, 0, self.view.width - imageV.right - 12- 15, 54)];
        _addressLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
        _addressLabel.font = kFont(14);
        NSString *region_name = _dataDic[@"REGION_NAME"];
        NSString *de = _dataDic[@"DE_ADDRESS"];
        if (!region_name.length && !de.length) {
            _addressLabel.text = @"未选地址";
        } else {
            _addressLabel.text = [NSString stringWithFormat:@"%@%@",_dataDic[@"REGION_NAME"],_dataDic[@"DE_ADDRESS"]];
        }
        [hViwe addSubview:_addressLabel];

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 54, kScreenWideth, 44)];
        view.backgroundColor = kWhiteColor;
        [hViwe addSubview:view];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 2 * kHalf, view.height)];
        label.text = @"订单汇总";
        label.textColor = COLORFROMCODE(0x4A4A4A, 1);
        label.font = kFont(13);
        [view addSubview:label];
        
        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWideth - 115, 0, 100, view.height)];
        _countLabel.textAlignment = NSTextAlignmentRight;
        NSInteger count = 0;
        for (NSDictionary *dic in _dataArray) {
            NSInteger qty = [dic[@"PROD_CNT"] integerValue];
            count += qty;
        }
        _countLabel.text = [NSString stringWithFormat:@"%ld件",count];
        _countLabel.textColor = COLORFROMCODE(0x4A4A4A, 1);
        _countLabel.font = kFont(13);
        [view addSubview:_countLabel];
        return hViwe;

    } else if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 16)];
        view.backgroundColor = kCColor;
        return view;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 98;
    } else if (section == 2){
        return 16;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        RMMOrderDetailGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OderCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dic = _dataArray[indexPath.row];
        return cell;
    } else if (indexPath.section == 1) {
        RMMOderYouHuiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YouCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dic = _dataDic;
        return cell;

    } else {
        RMMOderDeatilsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dic = _dataDic;
        return cell;

    }
}
- (void)doneButClick{
    [UIView animateWithDuration:kInterval animations:^{
        _pickerView.view.frame = CGRectMake(0, kScreenHeight, kScreenWideth, 249);
        _pickerView.backView.alpha = 0.0;
    } completion:^(BOOL finished) {
        _pickerView.hidden = YES;
        [self cancelOrderWith:_array[_row]];
    }];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _row = row;
}

- (void)commonAlertView:(RMMCommonAlertView *)commonAlertView didSelectItemAtIndex:(NSInteger)index{
}
// 支付宝支付
- (void)aliPay{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"order_no"] = _order_no;
    parDic[@"atm"] = @"0.01";
    NSString *url = GetALiPaySignUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NS_ADD(payJieGuo:, @"PAYJIEGUOFUWU", nil);
            [self aLipayWith:[self backStringWithDic:dataDic[@"data"]]];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)aLipayWith:(NSString *)sign{
        [[AlipaySDK defaultService] payOrder:sign fromScheme:ALiPaySchemes callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
}
- (NSString *)backStringWithDic:(NSDictionary *)dic{
    NSMutableArray *arr = [NSMutableArray array];
    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *string = [NSString stringWithFormat:@"%@=%@",(NSString *)key,(NSString *)obj];
        [arr addObject:string];
    }];
    NSString *str = [arr componentsJoinedByString:@"&"];
    return str;
}
- (void)payJieGuo:(NSNotification *)notification{
    NSDictionary *dic = [notification userInfo];
    NSString *status = dic[@"resultStatus"];
    if ([status isEqualToString:@"9000"]) {
        // 支付成功
        if (_isSuccess) {
            _isSuccess = NO;
            NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
            NSString *ID = @"";
            NSString *key = [NSString stringWithFormat:@"%@%@",cust_id,ID];
            [RMMFileManager removeObjForKey:key];
            RMMPayJieGuoViewController *VC = [[RMMPayJieGuoViewController alloc] init];
            VC.nameTitle = @"测试套餐";
            VC.balance = @"0.01";
            [self.navigationController pushViewController:VC animated:YES];
        }
    } else {
        [RMMFileManager MBProgressWith:dic[@"memo"] onView:self.view];
    }
}
- (void)callSkillBut{
    if (_phoneNumber.length) {
        [RMMFileManager callKeFuWithPhoneNumber:_phoneNumber onView:self.view];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
